/**
 * handles the farm scripting
 */
$(document).ready(function() {
	/*
	 * style buttons
	 */
	$('.uiButton').button();
	
	/**
	 * validate a staff form
	 */
	function validateStaffForm() {
		return true;
	}
	
	$('#btnCancel').click(function(){
		var qStaffPage = $("#qStaffPage").val();
		var id = $("#id").val();
		// Cancel from profile page
		if(qStaffPage == "profilePage"){
			if(window.confirm('Do you want to cancel this operation?')){
				window.location = "staff?action=view&item=staff&id="+id;
				return true;
			}else{
				return false;
			}
		}else{
			if(window.confirm('Do you want to cancel this operation?')){
				window.location = "staff?action=view";
				return true;
			}else{
				return false;
			}
		}
	});

	/*
	 * edit staff member
	 */
	$('#btnEdit').click(function() {
		var id = $("table.recordTable tr input:checked").attr("id");
		if(id != null){
			var href = $(this).attr("href");
			href = href + "&id="+id;
			$(this).attr("href", href);
			
			return true;
		}else{
			alert('please select a staff member first');
			return false;
		}
	});
	
	$('#btnDeleteFarm').click(function(){
		if($(":checked", "table.recordTable tbody tr").length > 0){
			if(window.confirm("Do you want to delete the selected farm(s)?")){
				var ids=$("input[name=selectedFarm]:checked").map(
					     function () {return this.value;}).get().join(",");
				var href = $(this).attr("href");
				href = href + "&farmIdsToDelete=" + ids;
				$(this).attr("href", href);

				return true;
			}
		}else{
			alert("please select a farm and try again")
		}
		
		return false;
	});
	
	$("#btnEditKraal").click(function(){
		var numberSelected = $(":checked", "table.recordTable tbody tr").length;
		if( numberSelected > 0){
			if(numberSelected == 1){
				var url = $(this).attr("href");
				url += "&kraalid="+$(":checked", "table.recordTable tbody tr").attr("value");
				
				window.location = url;
			}else{
				alert("please select only one Kraal and try again.")
			}
		}else{
			alert("please select an Kraal and try again")
		}
		
		return false;
	});
	
	$("#btnDeleteKraal").click(function() {
		if($(":checked", "table.recordTable tbody tr").length > 0){
			if(window.confirm("Do you want to delete the selected Kraal(s)?")){
				$.ajax({
					  type: 'POST',
					  url: $(this).attr("href"),
					  data: $("input[name='selectedKraal']").serialize(),
					  success: function(data, textStatus, xmlHttpRequest){
						  $(":checked", "table.recordTable tbody tr").each(function(){
							  var id = $(this).attr("value");
							  $("table.recordTable tbody tr#"+id).remove();
						  });
						  
						  setSystemMessage(xmlHttpRequest.responseText);
					  }
					});
			}
		}else{
			alert("please select a Kraal and try again")
		}
		
		return false;
	});
	
	$('#btnCancelAnimal').click(function(){
		var qStaffPage = $("#qpage").val();
		var kraalid = $("#id").val();
		var farmid = $("#farmid").val();
		var animalid = $("#animalid").val();
		// Cancel from profile page
		if(qStaffPage == "1"){
			if(window.confirm('Do you want to cancel this operation?')){
				window.location = "farm?action=view&item=animal&animalid="+animalid;
				return true;
			}else{
				return false;
			}
		}else{
			if(window.confirm('Do you want to cancel this operation?')){
				window.location = "farm?action=view&item=kraalanimals&kraalid="+kraalid;
				return true;
			}else{
				return false;
			}
		}
	});
	
	$("#btnDeleteAnimal").click(function() {
		if($(":checked", "table.recordTable tbody tr").length > 0){
			if(window.confirm("Do you want to delete the selected Animal(s)?")){
				$.ajax({
					  type: 'POST',
					  url: $(this).attr("href"),
					  data: $("input[name='selectedAnimal']").serialize(),
					  success: function(data, textStatus, xmlHttpRequest){
						  $(":checked", "table.recordTable tbody tr").each(function(){
							  var id = $(this).attr("value");
							  $("table.recordTable tbody tr#"+id).remove();
						  });
						  
						  setSystemMessage(xmlHttpRequest.responseText);
					  }
					});
			}
		}else{
			alert("please select a Kraal and try again")
		}
		
		return false;
	});
	
	$("#search-collapser").click(function(){
		if($("#search").is(":hidden")){
			$("#search").slideDown('slow');
		}else{
			$("#search").hide();
		}
	});
});